package com.headwind.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.headwind.entity.HwMood;
import com.headwind.mapper.HwMoodMapper;
import com.headwind.service.MybatisHwMoodService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/14
 * @description : 业务层实现
 */
@Service
@DS("slave")
public class MybatisHwMoodServiceImpl extends ServiceImpl<HwMoodMapper, HwMood>
        implements MybatisHwMoodService {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    /**
     * 发表说说
     *
     * @param mood 说说内容
     * @return 状态信息
     */
    @DS("master")
    @Override
    public Dict addMood(HwMood mood) {
        mood.setPublishTime(DateUtil.date());
        int recode = baseMapper.insert(mood);
        if (recode > 0) {
            return Dict.create().set("code", 201).set("message", "说说发表成功！");
        } else {
            return Dict.create().set("code", 500).set("message", "说说发表失败！");
        }
    }

    /**
     * 查找出大于等于特定ID的所有说说
     *
     * @return 说说列表
     */
    @Override
    public Dict findAllMoods(String id) {
        List<HwMood> result = baseMapper.selectList(Wrappers.<HwMood>lambdaQuery().ge(HwMood::getId, id));
        return Dict.create().set("code", 200).set("data", gson.toJson(result));
    }
}
